<template>
  <div class="system-dic-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1200px">
      <el-form
        ref="dicDialogFormRef"
        :model="state.ruleForm"
        :rules="formRules"
        label-width="140px"
        v-loading="formLoading"
      >
        <el-row :gutter="35">
          <el-col :span="12" class="mb20">
            <el-form-item label="服务名称" prop="name">
              <el-input v-model="state.ruleForm.name" placeholder="请输入服务名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="数据摘要" prop="digest">
              <el-input v-model="state.ruleForm.digest" placeholder="请输入数据摘要" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="关键词" prop="keyword">
              <el-input v-model="state.ruleForm.keyword" placeholder="请输入关键词" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="数据主题" prop="theme">
              <el-input v-model="state.ruleForm.theme" placeholder="请输入数据主题" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="时间分辨率" prop="time">
              <el-input v-model="state.ruleForm.time" placeholder="请输入时间分辨率（年份）" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="空间分辨率" prop="space">
              <el-input v-model="state.ruleForm.space" placeholder="请输入空间分辨率" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="数据描述语言" prop="language">
              <el-input v-model="state.ruleForm.language" placeholder="请输入数据描述语言" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="学科分类" prop="discipline">
              <el-input v-model="state.ruleForm.discipline" placeholder="请输入学科分类" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="数据格式" prop="format">
              <el-input v-model="state.ruleForm.format" placeholder="请输入数据格式" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="数据类型" prop="type">
              <el-input v-model="state.ruleForm.type" placeholder="请输入数据类型" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="表名称" prop="tbName">
              <el-input v-model="state.ruleForm.tbName" placeholder="请输入表名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="请求地址" prop="url">
              <el-input v-model="state.ruleForm.url" placeholder="请输入请求地址" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="onCancel">取 消</el-button>
        <el-button :disabled="formLoading" type="primary" @click="onSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { reactive, ref } from 'vue';
import { createApi, updateApi } from '@/api/ysj/index';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['success']);

const formLoading = ref(false);

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: undefined,
    name: '',
    digest: '',
    keyword: '',
    theme: '',
    time: '',
    space: '',
    language: '',
    discipline: '',
    format: '',
    type: '',
    gmId: undefined,
    tbName: '',
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
});

const formRules = reactive({
  name: [{ required: false, message: '不能为空', trigger: 'blur' }],
  digest: [{ required: false, message: '不能为空', trigger: 'blur' }],
  keyword: [{ required: false, message: '不能为空', trigger: 'blur' }],
  theme: [{ required: false, message: '不能为空', trigger: 'blur' }],
  time: [{ required: false, message: '不能为空', trigger: 'blur' }],
  space: [{ required: false, message: '不能为空', trigger: 'blur' }],
  language: [{ required: false, message: '不能为空', trigger: 'blur' }],
  discipline: [{ required: false, message: '不能为空', trigger: 'blur' }],
  format: [{ required: false, message: '不能为空', trigger: 'blur' }],
  type: [{ required: false, message: '不能为空', trigger: 'blur' }],
  tbName: [{ required: false, message: '不能为空', trigger: 'blur' }],
  url: [{ required: false, message: '不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
  state.dialog.type = type;
  resetForm();
  if (type === 'edit') {
    state.ruleForm = row;
    state.dialog.title = '修改';
    state.dialog.submitTxt = '修 改';
  } else {
    state.dialog.title = '新增';
    state.dialog.submitTxt = '新 增';
  }
  state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = async () => {
  formLoading.value = true;
  try {
    await dicDialogFormRef.value.validate();
    if (state.dialog.type === 'edit') {
      await updateApi(state.ruleForm);
    } else {
      await createApi(state.ruleForm);
    }
    closeDialog();
    emit('success');
    formLoading.value = false;
  } finally {
    formLoading.value = false;
  }
};

// 暴露变量
defineExpose({
  openDialog,
});

/** 重置表单 */
const resetForm = () => {
  state.ruleForm = {
    id: undefined,
    name: '',
    digest: '',
    keyword: '',
    theme: '',
    time: '',
    space: '',
    language: '',
    discipline: '',
    format: '',
    type: '',
    gmId: undefined,
    tbName: '',
  };
  // 清空表单，此项需加表单验证才能使用
  // nextTick(() => {
  //   dicDialogFormRef.value.resetFields();
  // });
};
</script>
